﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
手型工具类
*/



define(function (require, exports, module) {
    require('./Event.js');

    FXREADER.Event.CReader_HandToolHandler = function () {
        var _bMovePage = false,
             _bOldPosY = 0,
             _bOldPosX = 0,
             _bCursorInited = false,
             _pReaderApp = null;
        // Initialization
        this.Tool_OnInit = function (pReaderApp) {
            _pReaderApp = pReaderApp;
        };
        // Destory
        this.Tool_Destroy = function () {
        };
        // Name of the tool
        this.Tool_GetName = function () {
            return FXREADER.TOOL_NAME_HAND;
        };

        function _InitCursor() {
            if (_pReaderApp.GetMainView() != null) {
                if (!_bCursorInited) {
                    var pDocView = _pReaderApp.GetMainView().GetDocView();
                    var jqDocViewContainer = $("#" + pDocView.GetDocViewContainerID());
                    if (FXREADER.Environment.ie || FXREADER.Environment.ieAtLeast11)
                        jqDocViewContainer.addClass("fwr_hand_cursor_ie");
                    else
                        jqDocViewContainer.addClass("fwr_hand_cursor");
                    _bCursorInited = true;
                }
            }
            else {
                setTimeout(_InitCursor, 100);
            }

        }


        // UI Events
        this.Tool_OnActivate = function () {
            setTimeout(_InitCursor, 100);
            var pHandToolCommand = _pReaderApp.GetCommand("HandToolSelect");
            pHandToolCommand && pHandToolCommand.SetState(true);
        };
        this.Tool_OnDeactivate = function () {
            if (_pReaderApp.GetMainView() != null) {
                var pDocView = _pReaderApp.GetMainView().GetDocView();
                var jqDocViewContainer = $("#" + pDocView.GetDocViewContainerID());
                if (FXREADER.Environment.ie || FXREADER.Environment.ieAtLeast11)
                    jqDocViewContainer.removeClass("fwr_hand_cursor_ie");
                else
                    jqDocViewContainer.removeClass("fwr_hand_cursor");
            }
            _bCursorInited = false;
            var pHandToolCommand = _pReaderApp.GetCommand("HandToolSelect");
            pHandToolCommand && pHandToolCommand.SetState(false);
        };
        this.Tool_OnKeyDown = function (event) {
            return false;
        };
        this.Tool_OnKeyUp = function (event) {
            return false;
        };
        this.Tool_OnChar = function (event) {
            return false;
        };

        // Calculate status
        this.Tool_IsEnabled = function () {
            return true;
        };
        this.Tool_IsProcessing = function () {
            if (_bMovePage)
                return true;
            else
                return false;
        };

        // Mouse events
        this.Tool_OnLButtonDown = function (event) {
            var pDocView = _pReaderApp.GetMainView().GetDocView();
            var docViewRect = pDocView.GetDocViewClientRect();
            if (!FXREADER.RectUtils.PtInRect(docViewRect, event.pageX, event.pageY))
                return true;
            var jqDocViewContainer = $("#" + pDocView.GetDocViewContainerID());
            if (!FXREADER.Common.ContainsNode(jqDocViewContainer.get(0), event.target, true)) {
                return false;
            }
            if (FXREADER.Environment.ieAtLeast11)
                FXREADER.Common.PreventDefaults(event, true); //让鼠标在拖动时不会变成文本选中状态,同时解决ie11下面拖动页面不正常的bug
            _bMovePage = true;
            _bOldPosY = event.pageY; //当前鼠标位置
            _bOldPosX = event.pageX; //当前鼠标位置
            if (!(FXREADER.Environment.mobile || FXREADER.Environment.iOS)) 
            {
            	if (FXREADER.Environment.ie || FXREADER.Environment.ieAtLeast11) {
            	                jqDocViewContainer.addClass("fwr_catch_cursor_ie");
	                jqDocViewContainer.removeClass("fwr_hand_cursor_ie");
	            }
	            else {
	                jqDocViewContainer.addClass("fwr_catch_cursor");
	                jqDocViewContainer.removeClass("fwr_hand_cursor");
	            }
            }
            return true;
        };
        this.Tool_OnLButtonUp = function (event) {
            if (_bMovePage) {
                _bMovePage = false;
                var pDocView = _pReaderApp.GetMainView().GetDocView();
                var jqDocViewContainer = $("#" + pDocView.GetDocViewContainerID());
                if (!(FXREADER.Environment.mobile || FXREADER.Environment.iOS)) 
                {
	                if (FXREADER.Environment.ie || FXREADER.Environment.ieAtLeast11) {
	                    jqDocViewContainer.removeClass("fwr_catch_cursor_ie");
	                    jqDocViewContainer.addClass("fwr_hand_cursor_ie");
	                }
	                else {
	                    jqDocViewContainer.removeClass("fwr_catch_cursor");
	                    jqDocViewContainer.addClass("fwr_hand_cursor");
	                }
                }

            }
            return true;
        };
        this.Tool_OnLButtonDblClk = function (event) {
            return false;
        };
        this.Tool_OnMouseMove = function (event) {

            if (!_bMovePage) return true;
            //             if (FXREADER.Environment.ie) {
            //                 //ie需要调用PreventDefaults,要不然会导致页面无法拖动，而chrome调用PreventDefaults的话，会导致某些文本无法选中；
            // 
            //             } 
            FXREADER.Common.PreventDefaults(event, true);
            _pReaderApp.GetMainView().GetDocView().MovePage(_bOldPosX - event.pageX, _bOldPosY - event.pageY);
            _bOldPosY = event.pageY;
            _bOldPosX = event.pageX;
            return true;
        };
        this.Tool_OnRButtonDown = function (event) {
            return false;
        };
        this.Tool_OnRButtonUp = function (event) {
            return false;
        };
        this.Tool_OnRButtonDblClk = function (event) {
            return false;
        };
        this.Tool_OnMouseWheel = function (event) {
            return false;
        };
        this.Tool_OnMouseOver = function (event) {
            return false;
        };
        this.Tool_OnMouseOut = function (event) {
            return false;
        };
        this.Tool_OnMouseEnter = function (event) {
            return false;
        };
        this.Tool_OnMouseLeave = function (event) {
            if (_bMovePage) {
                _bMovePage = false;
            }
            return true;
        };

    };

    return FXREADER.Event.CReader_HandToolHandler;
});